package com.rtx.wms.std.adjustment.handle;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.wms.customer.k3.upload.adjustment.K3AdjustmentService;
import com.rtx.wms.std.adjustment.bean.Adjustment;
import com.rtx.wms.std.adjustment.bean.Adjustmentdetail;
import com.rtx.wms.std.adjustment.service.AdjustmentService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rtx.wms.std.common.bean.WmsConstant;
import com.rtx.wms.std.common.service.CommonService;

@Service
public class AdjustmentSchedulerHandle {
	
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private AdjustmentService adjustmentService;
	
	
    @Autowired
    private CommonService commonService;

	@Autowired
	private K3AdjustmentService  k3AdjustmentService;

	/**
	 * 处理待发送的调整单
	 * @param whseid
	 */
	public void handlerAdjustmentSend(String whseid){
		//获取待发送的调整单
		List<Adjustment> AdjustmentList = selectAdjustment(whseid);
		
		for(Adjustment a : AdjustmentList){
			List<Adjustmentdetail> detailList = a.getDetails();
			
			Adjustment adjustment1 = new Adjustment();
			BeanUtils.copyProperties(a,adjustment1);
			//加库存 盘亏单
			List<Adjustmentdetail> detailList1 = detailList.stream()
					.filter(detail -> "14001".equals(detail.getAdjustmentType()+""))
					.map(detail -> {detail.setAdjustmentkey(detail.getAdjustmentkey() + "-1");return detail;})
					.collect(Collectors.toList());
			if(detailList1.size() >0){
				
				Double totalQty = detailList1.stream().mapToDouble(Adjustmentdetail::getQty).sum(); 
				adjustment1.setAdjustmentkey(adjustment1.getAdjustmentkey()+"-1");
				adjustment1.setTotalQty(totalQty);
				adjustment1.setTotallines(detailList1.size());
				adjustment1.setDetails(detailList1);
				adjustment1.setRtxbsart("STK_StockCountLoss");
				
				//发送
				k3AdjustmentService.uploadHandle(adjustment1);
			}
			
			Adjustment adjustment2 = new Adjustment();
			BeanUtils.copyProperties(a,adjustment2);
			//减库存 盘盈单
			List<Adjustmentdetail> detailList2 = detailList.stream()
					.filter(detail -> "14002".equals(detail.getAdjustmentType()+""))
					.map(detail -> {detail.setAdjustmentkey(detail.getAdjustmentkey() + "-2");return detail;})
					.collect(Collectors.toList());
			if(detailList2.size() >0){
				Double totalQty = detailList2.stream().mapToDouble(Adjustmentdetail::getQty).sum();
				adjustment2.setAdjustmentkey(adjustment2.getAdjustmentkey()+"-2");
				adjustment1.setTotalQty(totalQty);
				adjustment1.setTotallines(detailList2.size());
				adjustment2.setDetails(detailList2);
				adjustment2.setRtxbsart("STK_StockCountGain");

				//发送
				k3AdjustmentService.uploadHandle(adjustment2);
			}
			//更新状态
			updateAdjustment(whseid,a);
		}
	}

	/**
	 * 更新调整单
	 * @param whseid
	 * @param a
	 */
	private void updateAdjustment(String whseid, Adjustment a) {
		LambdaUpdateWrapper<Adjustment> wrapper = Wrappers.lambdaUpdate();
		wrapper.eq(Adjustment::getAdjustmentkey,a.getAdjustmentkey())
				.set(Adjustment::getRtxedifinishflag,a.getRtxedifinishflag())
				.set(Adjustment::getMsg, StrUtil.sub(a.getMsg(),0,200));
		adjustmentService.updateByWrapper(whseid,wrapper);
	}

	/**
	 * 查询调整单
	 * @param whseid
	 * @return
	 */
	public List<Adjustment> selectAdjustment(String whseid){
		LambdaQueryWrapper<Adjustment> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(Adjustment::getStatus,"10").eq(Adjustment::getRtxedifinishflag, WmsConstant.DEFAULT_FLAG);
		return adjustmentService.selectAdjustmentByWrapper(whseid,queryWrapper);
	}
}
